
package de.imise.ur.ws;

//import java.text.SimpleDateFormat;
//import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import de.imise.ur.service.CreateUserService;

//import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;

/**
 * This endpoint handles holiday requests. It uses a combination of JDOM and XPath to extract interesting pieces of XML
 * from the incoming message, and invoked the injected {@link HumanResourceService} with those.
 *
 * @author Sebastian Kiunke
 */
@Endpoint
public class CreateUserEndpoint {

    private static final String NAMESPACE_URI = "http://imise.de/ur/schemas";

    private XPath usernameExpression;
    private XPath firstnameExpression;
    private XPath lastnameExpression;
    private XPath emailExpression;
    private XPath institutExpression;
    private XPath roleExpression;
    private XPath typeExpression;
    private XPath soapExpression;
    private XPath passwordExpression;
    
    private XPath studyExpression;
    
    private CreateUserService createUserService;

    
    @Autowired
    public CreateUserEndpoint(CreateUserService createUserService) throws JDOMException {
        this.createUserService = createUserService;
        Namespace namespace = Namespace.getNamespace("ur", NAMESPACE_URI);
        
        usernameExpression = XPath.newInstance("//ur:username");
        usernameExpression.addNamespace(namespace);
        
        firstnameExpression = XPath.newInstance("//ur:firstname");
        firstnameExpression.addNamespace(namespace);
        
        //lastnameExpression = XPath.newInstance("concat(//ur:FirstName,' ',//hr:LastName)");
        lastnameExpression = XPath.newInstance("//ur:lastname");
        lastnameExpression.addNamespace(namespace);
        
        emailExpression = XPath.newInstance("//ur:email");
        emailExpression.addNamespace(namespace);
        
        institutExpression = XPath.newInstance("//ur:institut");
        institutExpression.addNamespace(namespace);
        
        roleExpression = XPath.newInstance("//ur:role");
        roleExpression.addNamespace(namespace);
        
        typeExpression = XPath.newInstance("//ur:type");
        typeExpression.addNamespace(namespace);
        
        soapExpression = XPath.newInstance("//ur:soap");
        soapExpression.addNamespace(namespace);
        
        passwordExpression = XPath.newInstance("//ur:password");
        passwordExpression.addNamespace(namespace);
        
        studyExpression = XPath.newInstance("//ur:study-id");
        studyExpression.addNamespace(namespace);
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "UserRequest")
    @ResponsePayload
    public Element handleUserRequest(@RequestPayload Element userRequest) throws Exception {
        //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //Date startDate = dateFormat.parse(startDateExpression.valueOf(holidayRequest));

        String username = usernameExpression.valueOf(userRequest);
        String firstname = firstnameExpression.valueOf(userRequest);
        String lastname = lastnameExpression.valueOf(userRequest);
        String email = emailExpression.valueOf(userRequest);
        String institut = institutExpression.valueOf(userRequest);
        String role = roleExpression.valueOf(userRequest);
        String type = typeExpression.valueOf(userRequest);
        String soap = soapExpression.valueOf(userRequest);
        String password = passwordExpression.valueOf(userRequest);
        String study = studyExpression.valueOf(userRequest);

        return createUserService.createNewUser(username, firstname, lastname,
        								email, institut, role, type,
        								soap, password, study);
    }

}
